package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 完成文件的上传
     *
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult upload(MultipartFile multipartFile) throws IOException {
        //1. 把文件上传到minio得到文件的访问地址
        String filename = multipartFile.getOriginalFilename();  //文件原始的名字
        filename = UUID.randomUUID().toString().replaceAll("-", "") + filename.substring(filename.lastIndexOf("."));

        String path = fileStorageService.uploadImgFile("", filename, multipartFile.getInputStream());

        //2. 从ThreadLocal中获取当前登录的用户
        WmUser wmUser = WmThreadLocalUtil.get();

        //3. 准备数据插入数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUrl(path);
        wmMaterial.setUserId(wmUser.getId());
        wmMaterial.setType((short)0);
        wmMaterial.setIsCollection((short)0);

        //4. 返回结果数据
        save(wmMaterial);

        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 素材列表查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //1. 校验参数是否合法
        if(null == dto){
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //对分页参数进行校正
        dto.checkParam();

        //2. 设置查询的条件
        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(dto.getIsCollection() != null && dto.getIsCollection() == 1){
            lambdaQueryWrapper.eq(WmMaterial::getIsCollection, 1);
        }
        // 添加用户的id
        WmUser wmUser = WmThreadLocalUtil.get();
        lambdaQueryWrapper.eq(WmMaterial::getUserId, wmUser.getId());

        //按照时间进行降序排序
        lambdaQueryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        //3. 查询
        IPage<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());

        page = page(page, lambdaQueryWrapper);

        //4. 封装返回的结果
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int)page.getTotal());

        pageResponseResult.setData(page.getRecords());

        return pageResponseResult;
    }
}
